<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace addons\cloud_market\controller\api\developer;

use addons\cloud_market\controller\api\CloudMarketApiController;
use addons\cloud_market\model\CloudMarketAddon;
use addons\cloud_market\model\CloudMarketAddonVersions;
use addons\cloud_market\model\CloudMarketSite;
use hiduoke\services\FormBuilder as Form;

class Addon extends CloudMarketApiController
{
    protected $notApiCheck = ['*'];

    public $dev_id = 0;
    public $user_id = 0;

    public function initialize()
    {
        parent::initialize();
        $this->model = new CloudMarketAddon();
        $this->Site = new CloudMarketSite();
        $this->version_model = new CloudMarketAddonVersions();
        $user = $this->getUser();
        if (!$user['developer']) {
            showMessage('您没有权限', '', 'error');
        }
        $this->dev_id = $user['developer']['id'];
        $this->user_id = $user['id'];
    }

    /**
     * 【商城】获取列表
     */
    public function list()
    {
        $mix = [
            '$where' => ['user_id' => $this->user_id, 'dev_id' => $this->dev_id]
        ];
        $res = $this->model->getPageList($mix);
        return $this->success($res['count'], $res['data']->append(['label_names', 'new_version','buy_count','package_count']));
    }

    /**
     * 获取筛选
     */
    public function select()
    {
        $where = ['user_id' => $this->user_id, 'dev_id' => $this->dev_id];
        $res = $this->model->where($where)->select();
        $data = [];
        foreach ($res as $key => $value) {
            $data[] = [
                'label' => $value['title'],
                'value' => $value['id']
            ];
        }

        return $this->success('', $data);
    }

    public function getForm($id)
    {
        $row = $this->model->findOrEmpty($id);//创建表单
        $form = Form::createForm("/api.developer.addon/save?id=" . $id)->setMethod("POST");
        $disabled = false;
        if ($row['id']) {
            // 编辑
            $disabled = true;
        }
        $col2 = Form::col()->span(6);
        //新增表单
        $rule = [
            Form::input('title', '应用名称', $row['title'] ?? '')->required(),
            Form::input('name', '应用标识', $row['name'] ?? '')->disabled($disabled)
                ->effect(['help' => '创建后将无法修改当前应用标识'])->required(),
            // 类型
            Form::select('type', '应用类型', $row['type'] ?? '0')
                ->options([
                    ['label' => '系统', 'value' => '0'],
                    ['label' => '微信', 'value' => '1'],
                    ['label' => '小程序', 'value' => '2'],
                    ['label' => '企业微信', 'value' => '3'],
                    ['label' => '抖音', 'value' => '4'],
                    ['label' => '独立', 'value' => '5']
                ])->effect(['help' => '创建后将无法修改当前应用类型'])
                ->disabled($disabled),
            Form::input('remark', '备注', $row['remark'] ?? ''),
            Form::radio('status', '状态', $row['status'] ?? '0')
                ->options(
                    [
                        ['label' => '下架', 'value' => '-1'],
                        ['label' => '隐藏', 'value' => '0'],
                        ['label' => '正常', 'value' => '1'],
                        ['label' => '推荐', 'value' => '2', 'disabled' => true],
                    ]
                ),
            // 关键词
            Form::input('keyword', '关键词', $row['keyword'] ?? '')->required(),
            // 价格
            Form::number('price', '价格', $row['price'] ?? 0)->required(),
            // 服务费
            Form::number('service_price', '周期服务费', $row['service_price'] ?? 0)
                ->effect(['help' => '设置周期服务费，如果不收取，则设置为0即可，周期为一年，未续费将禁止应用更新'])->required(),
            Form::group('token_prices', '激活价格', $row['token_prices'] ?? [])->rule([
                Form::number('number', "", 1)->required()
                    ->effect(['help' => '设置当前激活数量'])->col($col2),
                Form::dkInput('price', "", 100, '设置当前套餐的付费价格')->required()->col($col2),
            ]),
            // 应用图标
            Form::dkImage('icon_image', '应用图标', $row['icon_image'] ?? '')->required(),
            // 应用截图
            Form::dkImageList('pic_images', '应用截图', $row['pic_images'] ?? []),
            // 应用介绍
            Form::textarea('description', '应用介绍', $row['description'] ?? '')->required(),
            // 应用详情
            Form::dkTinyEditor('detail_content', '应用详情', $row['detail_content'] ?? '')->required(),
        ];
        $title = !$id ? "新增" : "编辑";
        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    public function save($id = 0)
    {
        $row = $this->model->findOrEmpty($id);
        if ($id && $row['user_id'] != $this->user_id) {
            return $this->error('非法操作,你没有权限');
        }
        $formData = getFormData();
        if (!$id && $this->model->where('name', $formData['name'])->find()) {
            return $this->error('应用标识已存在');
        }
        $formData['user_id'] = $this->user_id;
        $formData['dev_id'] = $this->dev_id;
        $row->save($formData);
        return $this->success("保存成功", $formData);
    }

    public function del()
    {
        return $this->error('禁止删除');
        $id = input('id', 0);
        $row = $this->model->findOrEmpty($id);
        $row->delete();
        return $this->success("删除成功");
    }

}
